Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core): simplify cross-stack reference detection #11254

Closed
wants to merge 1 commit into from

Conversation

rix0rrr
Copy link
Contributor

@rix0rrr rix0rrr commented Nov 2, 2020

The current design of cross-stack references is needlessly
complicated.

It is based around being able to hook into the Token resolution
mechanism, which is then used to mock-resolve the entire construct tree
and remember all the CfnReference objects that were encountered while
doing so.

Then, we poke additional state into each of the encountered tokens so
that next time that same token object is resolved in the scope of a
consuming stack, we can output one of the poked values.

This whole solution introduces the following complications:

  • Scope-based resolution: instead of resolving to a simple value,
    the token executes code that inspects the scope of the resolver
    to return a different value.
  • Time-based resolution: depending on when the token is being
    resolved, it returns something different.

Both of these are the enemy of caching, and of consistency and
predictable behavior.

A better solution to always resolve to a consistent value that
represents the intended special semantics. In that way, the token itself
is trivial (all it needs to do is escape the type system and nothing
else), and template language postprocessors implement the special
features that are necessary.

For references, the issue is that the same token must sometimes
evaluate to a { Ref } intrinsic and sometimes must evaluate to a
{ Fn::ImportValue } intrinsic.

In this PR, we extend the template language that Tokens render to
from CloudFormation to CdkFormation, which is a superset of
CloudFormation that also contains the intrinsic:

{ "$Cdk::Ref": ["/path/to/node", "Attr"] }

This makes references be able to render to a stable value again,
independent of scope and time.

Finally, in stack.resolve() the function replaceReferences()
is called which translates from CdkFormation -> CloudFormation,
replacing { $Cdk::Ref } with { Ref } or { Fn::ImportValue }
as the case may be.

It has not yet been fully carried through in this PR, but this change
will be able to get rid of all the fields on IResolveContext and all
the resolution hook machinery that make the Token implementation
so complicated.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

The current design of cross-stack references is needlessly
complicated.

It is based around being able to hook into the Token resolution
mechanism, which is then used to mock-resolve the entire construct tree
and remember all the `CfnReference` objects that were encountered while
doing so.

Then, we poke additional state into each of the encountered tokens so
that *next time* that same token object is resolved in the scope of a
consuming stack, we can output one of the poked values.

This whole solution introduces the following complications:

- Scope-based resolution: instead of resolving to a simple value,
  the token executes code that inspects the scope of the resolver
  to return a different value.
- Time-based resolution: depending on when the token is being
  resolved, it returns something different.

Both of these are the enemy of caching, and of consistency and
predictable behavior.

A better solution to always resolve to a consistent value that
represents the intended special semantics. In that way, the token itself
is trivial (all it needs to do is escape the type system and nothing
else), and template language postprocessors implement the special
features that are necessary.

For references, the issue is that the same token must *sometimes*
evaluate to a `{ Ref }` intrinsic and sometimes must evaluate to a
`{ Fn::ImportValue }` intrinsic.

In this PR, we extend the template language that Tokens render to
from CloudFormation to **CdkFormation**, which is a superset of
CloudFormation that also contains the intrinsic:

```
{ "$Cdk::Ref": ["/path/to/node", "Attr"] }
```

This makes references be able to render to a stable value again,
independent of scope and time.

Finally, in `stack.resolve()` the function `replaceReferences()`
is called which translates from CdkFormation -> CloudFormation,
replacing `{ $Cdk::Ref }` with `{ Ref }` or `{ Fn::ImportValue }`
as the case may be.

It has not yet been fully carried through in this PR, but this change
will be able to get rid of all the fields on `IResolveContext` and all
the resolution hook machinery that make the Token implementation
so complicated.
@rix0rrr rix0rrr requested a review from a team November 2, 2020 14:12
@rix0rrr rix0rrr self-assigned this Nov 2, 2020
@gitpod-io
Copy link

gitpod-io bot commented Nov 2, 2020

@rix0rrr
Copy link
Contributor Author

rix0rrr commented Nov 2, 2020

There competing PRs out there that introduce race conditions with this PR. Specifically, there is work to be done in the JSONinification PR.

It should be clear that this PR is not production quality yet.

@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Nov 2, 2020
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: 04da9d0
  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

*/
public static forPseudo(pseudoName: string, scope: Construct) {
return CfnReference.singletonReference(scope, `Pseudo:${pseudoName}`, undefined, () => {
const cfnIntrinsic = { Ref: pseudoName };
const cfnIntrinsic = { '$Cdk::Ref': [scope.node.path, pseudoName] };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Define a const for "$CDK::Ref" and use it everywhere. Also add a comment explaining what is it

Suggested change
const cfnIntrinsic = { '$Cdk::Ref': [scope.node.path, pseudoName] };
const cfnIntrinsic = { '$CDK::Ref': [scope.node.path, pseudoName] };

if (typeof template === 'object' && template !== null) {
const keys = Object.keys(template);

if (keys.length === 1 && keys[0] === '$Cdk::Ref') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (keys.length === 1 && keys[0] === '$Cdk::Ref') {
if (keys.length === 1 && keys[0] === CDK_REF_KEY) {

@eladb eladb added the pr/do-not-merge This PR should not be merged at this time. label Nov 2, 2020
@rix0rrr rix0rrr removed their assignment Jun 22, 2021
@TheRealAmazonKendra
Copy link
Contributor

This PR has been deemed to be abandoned, and will be closed. Please create a new PR for these changes if you think this decision has been made in error.

@rix0rrr rix0rrr deleted the huijbers/stateless-reference-tokens branch July 4, 2022 07:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution/core This is a PR that came from AWS. pr/do-not-merge This PR should not be merged at this time.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants